<template>
  <div class="songsheet">
    <!-- 照片墙 -->
    <div class="photo">
      <div class="top">
        <div class="left" @click="goBack">
          <van-icon name="arrow-left" />
        </div>

        <div class="right" @click="showShare = true">
          <van-icon name="share-o" />
        </div>

        <!-- 分享面板 -->
        <van-share-sheet
          v-model="showShare"
          title="立即分享给好友"
          :options="options"
        />
      </div>

      <van-image :src="playlistData.coverImgUrl" lazy-load>
        <template v-slot:loading>
          <van-loading type="spinner" size="20" />
        </template>
      </van-image>
    </div>

    <!-- 歌单简介 -->
    <div class="photobox">
      <!-- 封图 -->
      <div class="img">
        <van-image :src="playlistData.coverImgUrl" lazy-load>
          <template v-slot:loading>
            <van-loading type="spinner" size="20" />
          </template>
        </van-image>
      </div>

      <div class="title">
        <!-- 歌单名 -->
        <div class="name">{{ playlistData.name }}</div>
        <!-- 简介 -->
        <div class="desc">{{ playlistData.description }}</div>
      </div>
    </div>

    <!-- 功能栏 -->
    <div class="function">
      <div class="list" v-if="!show" @click="addSheet">
        <van-icon name="add-o" />{{ dynamicData.bookedCount | playvolume }}
      </div>

      <div class="list" v-else>已收藏</div>

      <div class="list">
        <van-icon name="comment-o" @click="commentshow = true" />{{
          dynamicData.commentCount | playvolume
        }}
      </div>
    </div>

    <!-- 评论列表 -->
    <div class="comment">
      <van-popup
        v-model="commentshow"
        round
        position="bottom"
        :style="{ height: '80%' }"
      >
        <div class="commenttitle">精彩评论</div>

        <div class="commentmain">
          <div
            class="commentlist"
            v-for="(item, index) in commentData"
            :key="index"
          >
            <div class="listhead">
              <van-image
                width="50"
                height="50"
                round
                lazy-load
                :src="item.user.avatarUrl"
              >
                <template v-slot:loading>
                  <van-loading type="spinner" size="20" />
                </template>
              </van-image>
            </div>

            <div class="listtext">
              <div class="listtextbox">
                <div class="txtbox">
                  <div class="user">{{ item.user.nickname }}</div>
                  <div class="commenttime">{{ item.time | formatDate }}</div>
                </div>

                <div class="like">
                  <div class="liketxt">{{ item.likedCount }}</div>
                  <van-icon name="flower-o" />
                </div>
              </div>

              <div class="commenttext">{{ item.content }}</div>
            </div>
          </div>
        </div>
      </van-popup>
    </div>

    <!-- 歌曲列表 -->
    <div
      class="tabbox"
      v-for="(item, index) in tracksData"
      :key="index"
      @click="
        playAudio(
          item.id,
          item.al.picUrl,
          item.name,
          item.ar[0].name,
          item.ar[0].id
        )
      "
    >
      <!-- 封图 -->
      <div class="sealing">
        <van-image :src="item.al.picUrl" lazy-load>
          <template v-slot:loading>
            <van-loading type="spinner" size="20" />
          </template>
        </van-image>
      </div>

      <!-- 名字 -->
      <div class="tabname">
        <div class="tabsongname">{{ item.name }}</div>
        <div class="tabtxt">
          <div class="textbox">SQ</div>
          {{ item.ar[0].name }}
        </div>
      </div>

      <div class="play">
        <van-icon
          :name="
            audioId !== item.id
              ? 'play-circle-o'
              : isPaused
              ? 'play-circle-o'
              : 'pause-circle-o'
          "
        />
      </div>

      <div class="share" @click="showShare = true">
        <van-icon name="ellipsis" />
      </div>
    </div>

    <!-- 播放器 -->
    <Playbox></Playbox>
  </div>
</template>

<script>
import Playbox from "../components/Playbox.vue";
import { mapState, mapMutations } from "vuex";
export default {
  name: "Songsheet",

  components: {
    Playbox,
  },

  data() {
    return {
      show: false,

      //歌单数据
      playlistData: [],

      //歌单歌曲数据
      tracksData: [],

      //排行榜动态数据
      dynamicData: [],

      id: "",

      // 分享面板
      showShare: false,
      options: [
        [
          { name: "微信", icon: "wechat" },
          { name: "朋友圈", icon: "wechat-moments" },
          { name: "微博", icon: "weibo" },
          { name: "QQ", icon: "qq" },
        ],
        [
          { name: "复制链接", icon: "link" },
          { name: "分享海报", icon: "poster" },
          { name: "二维码", icon: "qrcode" },
          { name: "小程序码", icon: "weapp-qrcode" },
        ],
      ],

      //评论数据
      commentData: [],
      commentshow: false,
    };
  },

  computed: {
    ...mapState([
      "audioId",
      "audioEle",
      "isPaused",
      "songName",
      "songerName",
      "singerId",
      "audioComment",
      "Songsheet",
    ]),
  },

  created() {
    this.id = this.$route.params.id;

    //获取歌单详情数据
    this.getPlaylist();

    //获取歌单详情数据
    this.getDynamic();

    //获取评论数据
    this.getComment();
  },

  methods: {
    //返回上一级
    goBack() {
      this.$router.go(-1);
    },

    //收藏歌单
    addSheet() {
      if (this.$cookies.get("uid") === null) {
        this.$toast("请先登录");
      } else {
        this.show = true;
        let sheet = {
          name: this.playlistData.name,
          text: this.playlistData.description,
          img: this.playlistData.coverImgUrl,
          id: this.id,
        };

        this.addSongsheet(sheet);

        // 存放在本地储存中
        localStorage.setItem("Songsheet", JSON.stringify(this.Songsheet));
      }
    },

    //获取歌单详情数据
    getPlaylist() {
      this.axios({
        method: "get",
        url: "/playlist/detail",
        params: {
          id: this.id,
        },
      })
        .then((res) => {
          this.playlistData = res.data.playlist;
          this.tracksData = this.playlistData.tracks;
        })
        .catch((err) => {
          
        });
    },

    //获取歌单动态信息
    getDynamic() {
      this.axios({
        method: "get",
        url: "/playlist/detail/dynamic",
        params: {
          id: this.id,
        },
      })
        .then((res) => {
          this.dynamicData = res.data;
        })
        .catch((err) => {
          
        });
    },

    //获取评论数据
    getComment() {
      this.axios({
        method: "get",
        url: "/comment/playlist",
        params: {
          id: this.id,
        },
      })
        .then((res) => {
          this.commentData = res.data.hotComments;
        })
        .catch((err) => {
          
        });
    },

    ...mapMutations(["changeStateProperty", "addSongsheet"]),

    // 点击播放音频
    playAudio(id, picUrl, songName, songerName, singerId) {
      if (id === this.audioId) {
        if (!this.audioEle.paused) {
          //暂停歌曲
          this.audioEle.pause();
          this.changeStateProperty({ key: "isPaused", value: true });
        } else {
          //播放歌曲
          this.audioEle.play();
          this.changeStateProperty({ key: "isPaused", value: false });
        }
        return;
      }

      //获取音频url
      this.axios({
        method: "get",
        url: "/song/url",
        params: {
          id,
        },
      })
        .then((res) => {
          let audioUrl = res.data.data[0].url;
          this.changeStateProperty({ key: "audioUrl", value: audioUrl });
          this.changeStateProperty({ key: "audioId", value: id });
          //获取暂停状态
          this.changeStateProperty({ key: "isPaused", value: false });
        })
        .catch((err) => {
          
        });

      //获取播放图片
      this.changeStateProperty({ key: "picUrl", value: picUrl });

      this.changeStateProperty({ key: "songName", value: songName });
      this.changeStateProperty({ key: "songerName", value: songerName });
      this.changeStateProperty({ key: "singerId", value: singerId });

      //获取专辑评论数据
      this.axios({
        method: "get",
        url: "/comment/music",
        params: {
          id,
        },
      })
        .then((res) => {
          this.changeStateProperty({
            key: "audioComment",
            value: res.data.hotComments,
          });
        })
        .catch((err) => {
          
        });
    },
  },
};
</script>

<style lang="less" scoped>
// 照片墙
.photo {
  width: 100%;
  height: 230px;
}

.photo {
  &/deep/ .van-image {
    width: 100%;
    height: 230px;
    position: relative;
    bottom: 40px;
    z-index: -99;
    filter: blur(4px) brightness(0.5);
  }
}

.top {
  display: flex;
  padding: 10px 20px;
}

.left {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #fff;
}

.right {
  width: 20px;
  height: 20px;
  font-size: 20px;
  margin-left: auto;
  color: #fff;
}

//歌单简介
.photobox {
  width: 90%;
  margin: auto;
  padding: 10px 16px;
  display: flex;
  position: relative;
  bottom: 170px;
}

.img {
  width: 100px;
  height: 100px;
  border-radius: 10px;
}

.img {
  &/deep/ .van-image {
    width: 100px;
    height: 100px;
    border-radius: 10px;
    z-index: 99;
  }
}

.title {
  width: 60%;
  display: flex;
  flex-wrap: wrap;
  margin-left: 20px;
  align-items: center;
}

.name {
  width: 100%;
  color: #fff;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.desc {
  width: 100%;
  color: #d4d4ce;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: relative;
  bottom: 20px;
  right: 10px;
  transform: scale(0.9);
}

//功能栏
.function {
  width: 60%;
  border-radius: 20px;
  background-color: #fff;
  display: flex;
  margin: auto;
  padding: 10px 20px;
  margin-top: -136px;
}

.list {
  width: calc(100% / 2);
  height: 20px;
  line-height: 20px;
  text-align: center;
  border-right: 1px solid #dadada;
  &:nth-child(2) {
    border: 0;
  }
  &/deep/.van-icon {
    font-size: 18px;
    padding-right: 6px;
    top: 2px;
  }
}

//歌曲列表
.tabbox {
  width: 100%;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  padding: 0 14px;
  margin-top: 10px;
}

.sealing {
  width: 40px;
  height: 40px;
}

.sealing {
  &/deep/ .van-image {
    width: 40px;
    height: 40px;
  }
}

.tabname {
  width: 58%;
  display: flex;
  flex-wrap: wrap;
  padding-left: 6px;
}

.tabsongname {
  width: 100%;
  font-size: 14px;
  font-weight: 600;
  padding-left: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tabtxt {
  display: flex;
  color: #9f9f9f;
  align-items: center;
}

.textbox {
  width: 30px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border: 1px solid red;
  color: red;
  transform: scale(0.5);
}

.play {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: 40px;
}

.share {
  width: 20px;
  height: 20px;
  font-size: 20px;
  color: #9f9f9f;
  margin-left: auto;
}

// 评论列表
.commenttitle {
  width: 100%;
  font-size: 16px;
  font-weight: 600;
  padding: 10px 20px;
  box-sizing: border-box;
  position: fixed;
  background-color: #fff;
  z-index: 99;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
}

.commentmain {
  margin-top: 40px;
}

.commentlist {
  width: 100%;
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 10px 14px;
  border-bottom: 1px solid #9f9f9f3a;
}

.listhead {
  width: 50px;
}

.listtext {
  width: 82%;
}

.listtextbox {
  display: flex;
  align-items: center;
}

.like {
  font-size: 20px;
  margin-left: auto;
  display: flex;
  align-items: center;
}

.liketxt {
  font-size: 12px;
  color: #9f9f9f;
}

.commenttext {
  width: 100%;
  padding-top: 6px;
  font-size: 14px;
}
</style>